home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / BNU22SR1.ZIP / src / binutils.2 / bfd / coffgen.c < prev    next >
C/C++ Source or Header  |  1993-05-30  |  42KB  |  1,519 lines

  1. /* Support for the generic parts of COFF, for BFD.
  2.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Most of this hacked by  Steve Chamberlain, sac@cygnus.com.
  22.    Split out of coffcode.h by Ian Taylor, ian@cygnus.com.  */
  23.  
  24. /* This file contains COFF code that is not dependent on any
  25.    particular COFF target.  There is only one version of this file in
  26.    libbfd.a, so no target specific code may be put in here.  Or, to
  27.    put it another way,
  28.  
  29.    ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
  30.  
  31.    If you need to add some target specific behaviour, add a new hook
  32.    function to bfd_coff_backend_data.
  33.  
  34.    Some of these functions are also called by the ECOFF routines.
  35.    Those functions may not use any COFF specific information, such as
  36.    coff_data (abfd).  */
  37.  
  38. #include "bfd.h"
  39. #include "sysdep.h"
  40. #include "libbfd.h"
  41. #include "coff/internal.h"
  42. #include "seclet.h"
  43. #include "libcoff.h"
  44.  
  45. static asection bfd_debug_section = { "*DEBUG*" };
  46.  
  47. /* Take a section header read from a coff file (in HOST byte order),
  48.    and make a BFD "section" out of it.  This is used by ECOFF.  */
  49. static          boolean
  50. DEFUN(make_a_section_from_file,(abfd, hdr, target_index),
  51.       bfd            *abfd AND
  52.       struct internal_scnhdr  *hdr AND
  53.       unsigned int target_index)
  54. {
  55.   asection       *return_section;
  56.   char *name;
  57.     
  58.   /* Assorted wastage to null-terminate the name, thanks AT&T! */
  59.   name = bfd_alloc(abfd, sizeof (hdr->s_name)+1);
  60.   if (name == NULL) {
  61.       bfd_error = no_memory;
  62.       return false;
  63.     }
  64.   strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
  65.   name[sizeof (hdr->s_name)] = 0;
  66.  
  67.   return_section = bfd_make_section(abfd, name);
  68.   if (return_section == NULL)
  69.     return_section = bfd_coff_make_section_hook (abfd, name);
  70.   if (return_section == NULL)
  71.     return false;
  72.  
  73.   /* s_paddr is presumed to be = to s_vaddr */
  74.  
  75.   return_section->vma = hdr->s_vaddr;
  76.   return_section->_raw_size = hdr->s_size;
  77.   return_section->filepos = hdr->s_scnptr;
  78.   return_section->rel_filepos =  hdr->s_relptr;
  79.   return_section->reloc_count = hdr->s_nreloc;
  80.  
  81.   bfd_coff_set_alignment_hook (abfd, return_section, hdr);
  82.  
  83.   return_section->line_filepos =  hdr->s_lnnoptr;
  84.  
  85.   return_section->lineno_count = hdr->s_nlnno;
  86.   return_section->userdata = NULL;
  87.   return_section->next = (asection *) NULL;
  88.   return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr);
  89.  
  90.   return_section->target_index = target_index;
  91.  
  92.   /* At least on i386-coff, the line number count for a shared library
  93.      section must be ignored.  */
  94.   if ((return_section->flags & SEC_SHARED_LIBRARY) != 0)
  95.     return_section->lineno_count = 0;
  96.  
  97.   if (hdr->s_nreloc != 0)
  98.     return_section->flags |= SEC_RELOC;
  99.   /* FIXME: should this check 'hdr->s_size > 0' */
  100.   if (hdr->s_scnptr != 0)
  101.     return_section->flags |= SEC_HAS_CONTENTS;
  102.   return true;
  103. }
  104.  
  105. /* Read in a COFF object and make it into a BFD.  This is used by
  106.    ECOFF as well.  */
  107.  
  108. static
  109. bfd_target     *
  110. DEFUN(coff_real_object_p,(abfd, nscns, internal_f, internal_a),
  111.     bfd            *abfd AND
  112.     unsigned        nscns AND
  113.   struct internal_filehdr *internal_f AND
  114.   struct internal_aouthdr *internal_a)
  115. {
  116.   PTR tdata;
  117.   size_t          readsize;    /* length of file_info */
  118.   unsigned int scnhsz;
  119.   char *external_sections;
  120.  
  121.   /* Build a play area */
  122.   tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
  123.   if (tdata == NULL)
  124.     return 0;
  125.  
  126.   scnhsz = bfd_coff_scnhsz (abfd);
  127.   readsize = nscns * scnhsz;
  128.   external_sections = (char *)bfd_alloc(abfd, readsize);
  129.  
  130.   if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) {
  131.     goto fail;
  132.   }
  133.  
  134.   /* Now copy data as required; construct all asections etc */
  135.   if (nscns != 0) {
  136.     unsigned int    i;
  137.     for (i = 0; i < nscns; i++) {
  138.       struct internal_scnhdr tmp;
  139.       bfd_coff_swap_scnhdr_in(abfd, (PTR) (external_sections + i * scnhsz),
  140.                   (PTR) &tmp);
  141.       make_a_section_from_file(abfd,&tmp, i+1);
  142.     }
  143.   }
  144.  
  145. /*  make_abs_section(abfd);*/
  146.   
  147.   if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
  148.     goto fail;
  149.  
  150.   if (!(internal_f->f_flags & F_RELFLG))
  151.     abfd->flags |= HAS_RELOC;
  152.   if ((internal_f->f_flags & F_EXEC))
  153.     abfd->flags |= EXEC_P;
  154.   if (!(internal_f->f_flags & F_LNNO))
  155.     abfd->flags |= HAS_LINENO;
  156.   if (!(internal_f->f_flags & F_LSYMS))
  157.     abfd->flags |= HAS_LOCALS;
  158.  
  159.  
  160.   bfd_get_symcount(abfd) = internal_f->f_nsyms;
  161.   if (internal_f->f_nsyms)
  162.     abfd->flags |= HAS_SYMS;
  163.  
  164.   if (internal_a != (struct internal_aouthdr *) NULL)
  165.     bfd_get_start_address (abfd) = internal_a->entry;
  166.   else
  167.     bfd_get_start_address (abfd) = 0;
  168.  
  169.   return abfd->xvec;
  170.  fail:
  171.   bfd_release(abfd, tdata);
  172.   return (bfd_target *)NULL;
  173. }
  174.  
  175. /* Turn a COFF file into a BFD, but fail with wrong_format if it is
  176.    not a COFF file.  This is also used by ECOFF.  */
  177.  
  178. bfd_target *
  179. DEFUN(coff_object_p,(abfd),
  180.       bfd            *abfd)
  181. {
  182.   unsigned int filhsz;
  183.   unsigned int aoutsz;
  184.   int   nscns;
  185.   PTR filehdr;
  186.   struct internal_filehdr internal_f;
  187.   struct internal_aouthdr internal_a;
  188.  
  189.   bfd_error = system_call_error;
  190.  
  191.   /* figure out how much to read */
  192.   filhsz = bfd_coff_filhsz (abfd);
  193.   aoutsz = bfd_coff_aoutsz (abfd);
  194.  
  195.   filehdr = bfd_alloc (abfd, filhsz);
  196.   if (filehdr == NULL)
  197.     return 0;
  198.   if (bfd_read(filehdr, 1, filhsz, abfd) != filhsz)
  199.     return 0;
  200.   bfd_coff_swap_filehdr_in(abfd, filehdr, &internal_f);
  201.   bfd_release (abfd, filehdr);
  202.  
  203.   if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) {
  204.     bfd_error = wrong_format;
  205.     return 0;
  206.   }
  207.   nscns =internal_f.f_nscns;
  208.  
  209.   if (internal_f.f_opthdr) {
  210.     PTR opthdr;
  211.  
  212.     opthdr = bfd_alloc (abfd, aoutsz);
  213.     if (opthdr == NULL)
  214.       return 0;;
  215.     if (bfd_read(opthdr, 1,aoutsz, abfd) != aoutsz) {
  216.       return 0;
  217.     }
  218.     bfd_coff_swap_aouthdr_in(abfd, opthdr, (PTR)&internal_a);
  219.   }
  220.  
  221.   /* Seek past the opt hdr stuff */
  222.   bfd_seek(abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET);
  223.  
  224.   return coff_real_object_p(abfd, nscns, &internal_f,
  225.                 (internal_f.f_opthdr != 0
  226.                  ? &internal_a
  227.                  : (struct internal_aouthdr *) NULL));
  228. }
  229.  
  230. /* Get the BFD section from a COFF symbol section number.  */
  231.  
  232. struct sec *
  233. DEFUN(coff_section_from_bfd_index,(abfd, index),
  234.       bfd            *abfd AND
  235.       int             index)
  236. {
  237.   struct sec *answer = abfd->sections;
  238.  
  239.   if (index == N_ABS) 
  240.   {
  241.     return &bfd_abs_section;
  242.   }
  243.   if (index == N_UNDEF)
  244.   {
  245.     return &bfd_und_section;
  246.   }
  247.   if(index == N_DEBUG)
  248.   {
  249.     return &bfd_debug_section;
  250.     
  251.   }
  252.   
  253.   while (answer) {
  254.       if (answer->target_index == index)
  255.        return answer;
  256.       answer = answer->next;
  257.     }
  258.   BFD_ASSERT(0);
  259.   return &bfd_und_section;    /* For gcc -W and lint.  Never executed. */
  260. }
  261.  
  262. /* Get the upper bound of a COFF symbol table.  */
  263.  
  264. unsigned int
  265. coff_get_symtab_upper_bound(abfd)
  266. bfd            *abfd;
  267. {
  268.   if (!bfd_coff_slurp_symbol_table(abfd))
  269.     return 0;
  270.  
  271.   return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
  272. }
  273.  
  274.  
  275. /* Canonicalize a COFF symbol table.  */
  276.  
  277. unsigned int
  278. DEFUN(coff_get_symtab, (abfd, alocation),
  279.       bfd            *abfd AND
  280.       asymbol       **alocation)
  281. {
  282.     unsigned int    counter = 0;
  283.     coff_symbol_type *symbase;
  284.     coff_symbol_type **location = (coff_symbol_type **) (alocation);
  285.     if (!bfd_coff_slurp_symbol_table(abfd))
  286.      return 0;
  287.  
  288.     symbase = obj_symbols(abfd);
  289.     while (counter <  bfd_get_symcount(abfd))
  290.     {
  291.     /* This nasty code looks at the symbol to decide whether or
  292.        not it is descibes a constructor/destructor entry point. It
  293.        is structured this way to (hopefully) speed non matches */
  294. #if 0    
  295.     if (0 && symbase->symbol.name[9] == '$') 
  296.     {
  297.         bfd_constructor_entry(abfd, 
  298.                  (asymbol **)location,
  299.                   symbase->symbol.name[10] == 'I' ?
  300.                   "CTOR" : "DTOR");
  301.     }
  302. #endif
  303.     *(location++) = symbase++;
  304.     counter++;
  305.     }
  306.     *location++ = 0;
  307.     return bfd_get_symcount(abfd);
  308. }
  309.  
  310. /* Set lineno_count for the output sections of a COFF file.  */
  311.  
  312. int
  313. DEFUN(coff_count_linenumbers,(abfd),
  314.       bfd            *abfd)
  315. {
  316.   unsigned int    limit = bfd_get_symcount(abfd);
  317.   unsigned int    i;
  318.   int total = 0;
  319.   asymbol       **p;
  320.  {
  321.    asection       *s = abfd->sections->output_section;
  322.    while (s) {
  323.      BFD_ASSERT(s->lineno_count == 0);
  324.      s = s->next;
  325.    }
  326.  }
  327.  
  328.  
  329.   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
  330.     asymbol        *q_maybe = *p;
  331.     if (bfd_asymbol_flavour(q_maybe) == bfd_target_coff_flavour) {
  332.       coff_symbol_type *q = coffsymbol(q_maybe);
  333.       if (q->lineno) {
  334.     /*
  335.       This symbol has a linenumber, increment the owning
  336.       section's linenumber count
  337.       */
  338.     alent          *l = q->lineno;
  339.     q->symbol.section->output_section->lineno_count++;
  340.     total ++;
  341.     l++;
  342.     while (l->line_number) {
  343.       total ++;
  344.       q->symbol.section->output_section->lineno_count++;
  345.       l++;
  346.     }
  347.       }
  348.     }
  349.   }
  350.   return total;
  351. }
  352.  
  353. /* Takes a bfd and a symbol, returns a pointer to the coff specific
  354.    area of the symbol if there is one.  */
  355.  
  356. coff_symbol_type *
  357. DEFUN(coff_symbol_from,(ignore_abfd, symbol),
  358.       bfd            *ignore_abfd AND
  359.       asymbol        *symbol)
  360. {
  361.   if (bfd_asymbol_flavour(symbol) != bfd_target_coff_flavour)
  362.     return (coff_symbol_type *)NULL;
  363.  
  364.   if (bfd_asymbol_bfd(symbol)->tdata.coff_obj_data == (coff_data_type*)NULL)
  365.     return (coff_symbol_type *)NULL;
  366.  
  367.   return  (coff_symbol_type *) symbol;
  368. }
  369.  
  370. static void
  371. DEFUN(fixup_symbol_value,(coff_symbol_ptr, syment),
  372. coff_symbol_type *coff_symbol_ptr AND
  373. struct internal_syment *syment)
  374. {
  375.  
  376.   /* Normalize the symbol flags */
  377.   if (bfd_is_com_section (coff_symbol_ptr->symbol.section)) {
  378.     /* a common symbol is undefined with a value */
  379.     syment->n_scnum = N_UNDEF;
  380.     syment->n_value = coff_symbol_ptr->symbol.value;
  381.   }
  382.   else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
  383.     syment->n_value = coff_symbol_ptr->symbol.value;
  384.   }
  385.   else if (coff_symbol_ptr->symbol.section == & bfd_und_section) {
  386.     syment->n_scnum = N_UNDEF;
  387.     syment->n_value = 0;
  388.   }
  389.   else {
  390.     if (coff_symbol_ptr->symbol.section) {
  391.       syment->n_scnum     =
  392.        coff_symbol_ptr->symbol.section->output_section->target_index;
  393.  
  394.       syment->n_value =
  395.        coff_symbol_ptr->symbol.value +
  396.     coff_symbol_ptr->symbol.section->output_offset +
  397.      coff_symbol_ptr->symbol.section->output_section->vma;
  398.     }
  399.     else {
  400.     BFD_ASSERT(0);
  401.       /* This can happen, but I don't know why yet (steve@cygnus.com) */
  402.       syment->n_scnum = N_ABS;
  403.       syment->n_value = coff_symbol_ptr->symbol.value;
  404.     }
  405.   }
  406. }
  407.  
  408. /* run through all the symbols in the symbol table and work out what
  409.    their indexes into the symbol table will be when output
  410.  
  411.  Coff requires that each C_FILE symbol points to the next one in the
  412.  chain, and that the last one points to the first external symbol. We
  413.  do that here too.
  414.  
  415. */
  416. void
  417. DEFUN(coff_renumber_symbols,(bfd_ptr),
  418.       bfd *bfd_ptr)
  419. {
  420.   unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
  421.   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
  422.   unsigned int native_index = 0;
  423.   struct internal_syment *last_file = (struct internal_syment *)NULL;
  424.   unsigned int symbol_index;
  425.  
  426.   /* COFF demands that undefined symbols come after all other symbols.
  427.      Since we don't need to impose this extra knowledge on all our client
  428.      programs, deal with that here.  Sort the symbol table; just move the
  429.      undefined symbols to the end, leaving the rest alone.  */
  430.   /* @@ Do we have some condition we could test for, so we don't always
  431.      have to do this?  I don't think relocatability is quite right, but
  432.      I'm not certain.  [raeburn:19920508.1711EST]  */
  433.   {
  434.     asymbol **newsyms;
  435.     int i;
  436.  
  437.     newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
  438.                         sizeof (asymbol *)
  439.                         * (symbol_count + 1));
  440.     bfd_ptr->outsymbols = newsyms;
  441.     for (i = 0; i < symbol_count; i++)
  442.       if (symbol_ptr_ptr[i]->section != &bfd_und_section)
  443.     *newsyms++ = symbol_ptr_ptr[i];
  444.     for (i = 0; i < symbol_count; i++)
  445.       if (symbol_ptr_ptr[i]->section == &bfd_und_section)
  446.     *newsyms++ = symbol_ptr_ptr[i];
  447.     *newsyms = (asymbol *) NULL;
  448.     symbol_ptr_ptr = bfd_ptr->outsymbols;
  449.   }
  450.  
  451.   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
  452.       {
  453.     coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
  454.     if (coff_symbol_ptr && coff_symbol_ptr->native) {
  455.       combined_entry_type *s = coff_symbol_ptr->native;
  456.       int i;
  457.  
  458.       if (s->u.syment.n_sclass == C_FILE)
  459.           {
  460.         if (last_file != (struct internal_syment *)NULL) {
  461.           last_file->n_value = native_index;
  462.         }
  463.         last_file = &(s->u.syment);
  464.           }
  465.       else {
  466.  
  467.         /* Modify the symbol values according to their section and
  468.            type */
  469.  
  470.         fixup_symbol_value(coff_symbol_ptr, &(s->u.syment));
  471.       }
  472.       for (i = 0; i < s->u.syment.n_numaux + 1; i++) {
  473.         s[i].offset = native_index ++;
  474.       }
  475.     }
  476.     else {
  477.       native_index++;
  478.     }
  479.       }
  480.   obj_conv_table_size (bfd_ptr) = native_index;
  481. }
  482.  
  483. /*
  484.  Run thorough the symbol table again, and fix it so that all pointers to
  485.  entries are changed to the entries' index in the output symbol table.
  486.  
  487. */
  488. void
  489. DEFUN(coff_mangle_symbols,(bfd_ptr),
  490.       bfd *bfd_ptr)
  491. {
  492.   unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
  493.   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
  494.   unsigned int symbol_index;
  495.  
  496.   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
  497.       {
  498.     coff_symbol_type *coff_symbol_ptr =
  499.       coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
  500.  
  501.     if (coff_symbol_ptr && coff_symbol_ptr->native) {
  502.       int i;
  503.       combined_entry_type *s = coff_symbol_ptr->native;
  504.  
  505.       for (i = 0; i < s->u.syment.n_numaux ; i++) {
  506.         combined_entry_type *a = s + i + 1;
  507.         if (a->fix_tag) {
  508.           a->u.auxent.x_sym.x_tagndx.l =
  509.         a->u.auxent.x_sym.x_tagndx.p->offset;
  510.           a->fix_tag = 0;
  511.         }
  512.         if (a->fix_end) {
  513.           a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
  514.         a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
  515.           a->fix_end = 0;
  516.           
  517.         }
  518.  
  519.       }
  520.     }
  521.       }
  522. }
  523.  
  524. static int string_size;
  525.  
  526. static void
  527. DEFUN(coff_fix_symbol_name,(abfd, symbol, native),
  528.   bfd *abfd AND
  529.   asymbol *symbol AND
  530.   combined_entry_type *native)
  531. {
  532.   unsigned int    name_length;
  533.   union internal_auxent *auxent;
  534.   char *  name = ( char *)(symbol->name);
  535.  
  536.   if (name == (char *) NULL) {
  537.     /* coff symbols always have names, so we'll make one up */
  538.     symbol->name = "strange";
  539.     name = (char *)symbol->name;
  540.   }
  541.   name_length = strlen(name);
  542.  
  543.   if (native->u.syment.n_sclass == C_FILE) {
  544.     strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN);
  545.     auxent = &(native+1)->u.auxent;
  546.  
  547.     if (bfd_coff_long_filenames (abfd)) {
  548.       if (name_length <= FILNMLEN) {
  549.     strncpy(auxent->x_file.x_fname, name, FILNMLEN);
  550.       }
  551.       else {
  552.     auxent->x_file.x_n.x_offset = string_size + 4;
  553.     auxent->x_file.x_n.x_zeroes = 0;
  554.     string_size += name_length + 1;
  555.       }
  556.     }
  557.     else {
  558.       strncpy(auxent->x_file.x_fname, name, FILNMLEN);
  559.       if (name_length > FILNMLEN) {
  560.     name[FILNMLEN] = '\0';
  561.       }
  562.     }
  563.   }
  564.   else
  565.       {                /* NOT A C_FILE SYMBOL */
  566.     if (name_length <= SYMNMLEN) {
  567.       /* This name will fit into the symbol neatly */
  568.       strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN);
  569.     }
  570.     else {
  571.       native->u.syment._n._n_n._n_offset =  string_size + 4;
  572.       native->u.syment._n._n_n._n_zeroes = 0;
  573.       string_size += name_length + 1;
  574.     }
  575.       }
  576. }
  577.  
  578. #define    set_index(symbol, idx)    ((symbol)->udata =(PTR) (idx))
  579.  
  580. static unsigned int
  581. DEFUN(coff_write_symbol,(abfd, symbol, native, written),
  582. bfd *abfd AND
  583. asymbol *symbol AND
  584. combined_entry_type *native AND
  585. unsigned int written)
  586. {
  587.   unsigned int    numaux = native->u.syment.n_numaux;
  588.   int             type = native->u.syment.n_type;
  589.   int             class =  native->u.syment.n_sclass;
  590.   PTR buf;
  591.   bfd_size_type symesz;
  592.  
  593.   /* @@ bfd_debug_section isn't accessible outside this file, but we know
  594.      that C_FILE symbols belong there.  So move them.  */
  595.   if (native->u.syment.n_sclass == C_FILE)
  596.     symbol->section = &bfd_debug_section;
  597.  
  598.   if (symbol->section == &bfd_abs_section) 
  599.   {
  600.     native->u.syment.n_scnum = N_ABS;
  601.   }
  602.   else if (symbol->section == &bfd_debug_section) 
  603.   {
  604.     native->u.syment.n_scnum = N_DEBUG;
  605.   }
  606.   else if (symbol->section == &bfd_und_section)   
  607.   {
  608.     native->u.syment.n_scnum = N_UNDEF;
  609.   }
  610.   else 
  611.   {
  612.     native->u.syment.n_scnum =
  613.      symbol->section->output_section->target_index;
  614.   }
  615.   
  616.   
  617.   coff_fix_symbol_name(abfd, symbol, native);
  618.  
  619.   symesz = bfd_coff_symesz (abfd);
  620.   buf = bfd_alloc (abfd, symesz);
  621.   bfd_coff_swap_sym_out(abfd, &native->u.syment, buf);
  622.   bfd_write(buf, 1, symesz, abfd);
  623.   bfd_release (abfd, buf);
  624.  
  625.   if (native->u.syment.n_numaux > 0)
  626.     {
  627.       bfd_size_type auxesz;
  628.       unsigned int j;
  629.  
  630.       auxesz = bfd_coff_auxesz (abfd);
  631.       buf = bfd_alloc (abfd, auxesz);
  632.       for (j = 0; j < native->u.syment.n_numaux;  j++)
  633.     {
  634.       bfd_coff_swap_aux_out(abfd,
  635.                 &((native + j + 1)->u.auxent),
  636.                 type,
  637.                 class,
  638.                 buf);
  639.       bfd_write(buf, 1, auxesz, abfd);
  640.     }
  641.       bfd_release (abfd, buf);
  642.     }
  643.   /*
  644.     Reuse somewhere in the symbol to keep the index
  645.     */
  646.   set_index(symbol, written);
  647.   return   written + 1 + numaux;
  648. }
  649.  
  650.  
  651. static unsigned int
  652. DEFUN(coff_write_alien_symbol,(abfd, symbol, written),
  653.       bfd *abfd AND
  654.       asymbol *symbol AND
  655.       unsigned int written)
  656. {
  657.   /*
  658.     This symbol has been created by the loader, or come from a non
  659.     coff format. It  has no native element to inherit, make our
  660.     own
  661.     */
  662.   combined_entry_type *native;
  663.   combined_entry_type dummy;
  664.   native = &dummy;
  665.   native->u.syment.n_type =  T_NULL;
  666.   native->u.syment.n_flags =  0;
  667.   if (symbol->section == &bfd_und_section) 
  668.   {
  669.       native->u.syment.n_scnum =  N_UNDEF;
  670.       native->u.syment.n_value =  symbol->value;
  671.     }
  672.   else if (bfd_is_com_section (symbol->section))
  673.   {
  674.       native->u.syment.n_scnum =  N_UNDEF;
  675.       native->u.syment.n_value =  symbol->value;
  676.  
  677.   }
  678.   
  679.   else if (symbol->flags & BSF_DEBUGGING) {
  680.       /*
  681.     remove name so it doesn't take up any space
  682.     */
  683.       symbol->name = "";
  684.     }
  685.   else {
  686.       native->u.syment.n_scnum  =   symbol->section->output_section->target_index;
  687.       native->u.syment.n_value =   symbol->value +
  688.        symbol->section->output_section->vma +
  689.     symbol->section->output_offset;
  690.       /* Copy the any flags from the the file hdr into the symbol  */
  691.     {
  692.       coff_symbol_type *c = coff_symbol_from(abfd, symbol);
  693.       if (c != (coff_symbol_type *)NULL) {
  694.       native->u.syment.n_flags =   bfd_asymbol_bfd(&c->symbol)->flags;
  695.     }
  696.     }
  697.     }
  698.  
  699.   native->u.syment.n_type =  0;
  700.   if (symbol->flags & BSF_LOCAL)
  701.    native->u.syment.n_sclass =  C_STAT;
  702.   else
  703.    native->u.syment.n_sclass =  C_EXT;
  704.   native->u.syment.n_numaux =  0;
  705.  
  706.   return   coff_write_symbol(abfd, symbol, native, written);
  707. }
  708.  
  709. static unsigned int
  710. DEFUN(coff_write_native_symbol,(abfd, symbol,   written),
  711. bfd *abfd AND
  712. coff_symbol_type *symbol AND
  713. unsigned int written)
  714. {
  715.   /*
  716.     Does this symbol have an ascociated line number - if so then
  717.     make it remember this symbol index. Also tag the auxent of
  718.     this symbol to point to the right place in the lineno table
  719.     */
  720.   combined_entry_type *native = symbol->native;
  721.  
  722.   alent          *lineno = symbol->lineno;
  723.  
  724.   if (lineno && !symbol->done_lineno) {
  725.     unsigned int    count = 0;
  726.     lineno[count].u.offset = written;
  727.     if (native->u.syment.n_numaux) {
  728.       union internal_auxent  *a = &((native+1)->u.auxent);
  729.  
  730.       a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
  731.     symbol->symbol.section->output_section->moving_line_filepos;
  732.     }
  733.     /*
  734.       And count and relocate all other linenumbers
  735.       */
  736.  
  737.     count++;
  738.     while (lineno[count].line_number) {
  739. #if 0
  740. /* 13 april 92. sac 
  741. I've been told this, but still need proof:
  742. > The second bug is also in `bfd/coffcode.h'.  This bug causes the linker to screw
  743. > up the pc-relocations for all the line numbers in COFF code.  This bug isn't
  744. > only specific to A29K implementations, but affects all systems using COFF
  745. > format binaries.  Note that in COFF object files, the line number core offsets
  746. > output by the assembler are relative to the start of each procedure, not
  747. > to the start of the .text section.  This patch relocates the line numbers
  748. > relative to the `native->u.syment.n_value' instead of the section virtual
  749. > address.  modular!olson@cs.arizona.edu (Jon Olson)
  750. */
  751.        lineno[count].u.offset += native->u.syment.n_value;
  752.  
  753. #else
  754.       lineno[count].u.offset +=
  755.     symbol->symbol.section->output_section->vma +
  756.       symbol->symbol.section->output_offset;
  757. #endif
  758.       count++;
  759.     }
  760.     symbol->done_lineno = true;
  761.     
  762.     symbol->symbol.section->output_section->moving_line_filepos +=
  763.       count * bfd_coff_linesz (abfd);
  764.   }
  765.   return coff_write_symbol(abfd, &( symbol->symbol), native,written);
  766. }
  767.  
  768. void
  769. DEFUN(coff_write_symbols,(abfd),
  770.       bfd            *abfd)
  771. {
  772.   unsigned int    i;
  773.   unsigned int    limit = bfd_get_symcount(abfd);
  774.   unsigned int    written = 0;
  775.  
  776.   asymbol       **p;
  777.  
  778.   string_size = 0;
  779.  
  780.  
  781.   /* Seek to the right place */
  782.   bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
  783.  
  784.   /* Output all the symbols we have */
  785.  
  786.   written = 0;
  787.   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
  788.       {
  789.     asymbol        *symbol = *p;
  790.     coff_symbol_type *c_symbol = coff_symbol_from(abfd, symbol);
  791.  
  792.     if (c_symbol == (coff_symbol_type *) NULL ||
  793.         c_symbol->native == (combined_entry_type *)NULL)
  794.         {
  795.           written = coff_write_alien_symbol(abfd, symbol, written);
  796.         }
  797.     else
  798.         {
  799.           written = coff_write_native_symbol(abfd, c_symbol, written);
  800.         }
  801.  
  802.       }
  803.  
  804.   bfd_get_symcount(abfd) = written;
  805.  
  806.   /* Now write out strings */
  807.  
  808.   if (string_size != 0)
  809.    {
  810.      unsigned int    size = string_size + 4;
  811.      bfd_byte buffer[4];
  812.  
  813.      bfd_h_put_32(abfd, size, buffer);
  814.      bfd_write((PTR) buffer, 1, sizeof(buffer), abfd);
  815.      for (p = abfd->outsymbols, i = 0;
  816.       i < limit;
  817.       i++, p++)
  818.      {
  819.        asymbol        *q = *p;
  820.        size_t          name_length = strlen(q->name);
  821.        int maxlen;
  822.        coff_symbol_type*       c_symbol = coff_symbol_from(abfd, q);
  823.        maxlen = ((c_symbol != NULL && c_symbol->native != NULL) &&
  824.              (c_symbol->native->u.syment.n_sclass == C_FILE)) ?
  825.          FILNMLEN : SYMNMLEN;
  826.  
  827.        if (name_length > maxlen) {
  828.          bfd_write((PTR) (q->name), 1, name_length + 1, abfd);
  829.        }
  830.      }
  831.    }
  832.   else {
  833.     /* We would normally not write anything here, but we'll write
  834.        out 4 so that any stupid coff reader which tries to read
  835.        the string table even when there isn't one won't croak.
  836.        */
  837.  
  838.     uint32e_type size = 4;
  839.     size =  size;
  840.     bfd_write((PTR)&size, 1, sizeof(size), abfd);
  841.  
  842.   }
  843. }
  844.  
  845. void
  846. DEFUN(coff_write_linenumbers,(abfd),
  847.       bfd            *abfd)
  848. {
  849.   asection       *s;
  850.   bfd_size_type linesz;
  851.   PTR buff;
  852.  
  853.   linesz = bfd_coff_linesz (abfd);
  854.   buff = bfd_alloc (abfd, linesz);
  855.   for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
  856.     if (s->lineno_count) {
  857.       asymbol       **q = abfd->outsymbols;
  858.       bfd_seek(abfd, s->line_filepos, SEEK_SET);
  859.       /* Find all the linenumbers in this section */
  860.       while (*q) {
  861.     asymbol        *p = *q;
  862.     if (p->section->output_section == s) {
  863.       alent          *l =
  864.        BFD_SEND(bfd_asymbol_bfd(p), _get_lineno, (bfd_asymbol_bfd(p), p));
  865.       if (l) {
  866.         /* Found a linenumber entry, output */
  867.         struct internal_lineno  out;
  868.         memset( (PTR)&out, 0, sizeof(out));
  869.         out.l_lnno = 0;
  870.         out.l_addr.l_symndx = l->u.offset;
  871.         bfd_coff_swap_lineno_out(abfd, &out, buff);
  872.         bfd_write(buff, 1, linesz, abfd);
  873.         l++;
  874.         while (l->line_number) {
  875.           out.l_lnno = l->line_number;
  876.           out.l_addr.l_symndx = l->u.offset;
  877.           bfd_coff_swap_lineno_out(abfd, &out, buff);
  878.           bfd_write(buff, 1, linesz, abfd);
  879.           l++;
  880.         }
  881.       }
  882.     }
  883.     q++;
  884.       }
  885.     }
  886.   }
  887.   bfd_release (abfd, buff);
  888. }
  889.  
  890. alent   *
  891. DEFUN(coff_get_lineno,(ignore_abfd, symbol),
  892.       bfd            *ignore_abfd AND
  893.       asymbol        *symbol)
  894. {
  895.   return coffsymbol(symbol)->lineno;
  896. }
  897.  
  898. asymbol *
  899. coff_section_symbol (abfd, name)
  900.      bfd *abfd;
  901.      char *name;
  902. {
  903.   asection *sec = bfd_make_section_old_way (abfd, name);
  904.   asymbol *sym;
  905.   combined_entry_type *csym;
  906.  
  907.   sym = sec->symbol;
  908.   if (coff_symbol_from (abfd, sym))
  909.     csym = coff_symbol_from (abfd, sym)->native;
  910.   else
  911.     csym = 0;
  912.   /* Make sure back-end COFF stuff is there.  */
  913.   if (csym == 0)
  914.     {
  915.       struct foo {
  916.     coff_symbol_type sym;
  917.     /* @@FIXME This shouldn't use a fixed size!!  */
  918.     combined_entry_type e[10];
  919.       };
  920.       struct foo *f;
  921.       f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
  922.       memset ((char *) f, 0, sizeof (*f));
  923.       coff_symbol_from (abfd, sym)->native = csym = f->e;
  924.     }
  925.   csym[0].u.syment.n_sclass = C_STAT;
  926.   csym[0].u.syment.n_numaux = 1;
  927. /*  SF_SET_STATICS (sym);    @@ ??? */
  928.   if (sec)
  929.     {
  930.       csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
  931.       csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
  932.       csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
  933.     }
  934.   else
  935.     {
  936.       csym[1].u.auxent.x_scn.x_scnlen = 0;
  937.       csym[1].u.auxent.x_scn.x_nreloc = 0;
  938.       csym[1].u.auxent.x_scn.x_nlinno = 0;
  939.     }
  940.   return sym;
  941. }
  942.  
  943. /* This function transforms the offsets into the symbol table into
  944.    pointers to syments.  */
  945.  
  946. static void
  947. DEFUN(coff_pointerize_aux,(abfd, table_base, type, class, auxent),
  948. bfd *abfd AND
  949. combined_entry_type *table_base AND
  950. int type AND
  951. int class AND
  952. combined_entry_type *auxent)
  953. {
  954.   /* Don't bother if this is a file or a section */
  955.   if (class == C_STAT && type == T_NULL) return;
  956.   if (class == C_FILE) return;
  957.  
  958.   /* Otherwise patch up */
  959. #define N_TMASK coff_data (abfd)->local_n_tmask
  960. #define N_BTSHFT coff_data (abfd)->local_n_btshft
  961.   if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) {
  962.       auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base +
  963.        auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
  964.       auxent->fix_end = 1;
  965.     }
  966.   /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
  967.      generate one, so we must be careful to ignore it.  */
  968.   if (auxent->u.auxent.x_sym.x_tagndx.l > 0) {
  969.       auxent->u.auxent.x_sym.x_tagndx.p =
  970.        table_base +  auxent->u.auxent.x_sym.x_tagndx.l;
  971.       auxent->fix_tag = 1;
  972.     }
  973. }
  974.  
  975. static char *
  976. DEFUN(build_string_table,(abfd),
  977. bfd *abfd)
  978. {
  979.   char string_table_size_buffer[4];
  980.   unsigned int string_table_size;
  981.   char *string_table;
  982.  
  983.   /* At this point we should be "seek"'d to the end of the
  984.      symbols === the symbol table size.  */
  985.   if (bfd_read((char *) string_table_size_buffer,
  986.            sizeof(string_table_size_buffer),
  987.            1, abfd) != sizeof(string_table_size)) {
  988.     bfd_error = system_call_error;
  989.     return (NULL);
  990.   }                /* on error */
  991.  
  992.   string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer);
  993.  
  994.   if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
  995.     bfd_error = no_memory;
  996.     return (NULL);
  997.   }                /* on mallocation error */
  998.   if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) {
  999.     bfd_error = system_call_error;
  1000.     return (NULL);
  1001.   }
  1002.   return string_table;
  1003. }
  1004.  
  1005. /* Allocate space for the ".debug" section, and read it.
  1006.    We did not read the debug section until now, because
  1007.    we didn't want to go to the trouble until someone needed it. */
  1008.  
  1009. static char *
  1010. DEFUN(build_debug_section,(abfd),
  1011.     bfd *abfd)
  1012. {
  1013.   char *debug_section;
  1014.   long position;
  1015.  
  1016.   asection *sect = bfd_get_section_by_name (abfd, ".debug");
  1017.  
  1018.   if (!sect) {
  1019.      bfd_error = no_debug_section;
  1020.      return NULL;
  1021.   }
  1022.  
  1023.   debug_section = (PTR) bfd_alloc (abfd,
  1024.                    bfd_get_section_size_before_reloc (sect));
  1025.   if (debug_section == NULL) {
  1026.     bfd_error = no_memory;
  1027.     return NULL;
  1028.   }
  1029.  
  1030.   /* Seek to the beginning of the `.debug' section and read it. 
  1031.      Save the current position first; it is needed by our caller.
  1032.      Then read debug section and reset the file pointer.  */
  1033.  
  1034.   position = bfd_tell (abfd);
  1035.   bfd_seek (abfd, sect->filepos, SEEK_SET);
  1036.   if (bfd_read (debug_section, 
  1037.         bfd_get_section_size_before_reloc (sect), 1, abfd)
  1038.       != bfd_get_section_size_before_reloc(sect)) {
  1039.     bfd_error = system_call_error;
  1040.     return NULL;
  1041.   }
  1042.   bfd_seek (abfd, position, SEEK_SET);
  1043.   return debug_section;
  1044. }
  1045.  
  1046.  
  1047. /* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
  1048.  \0-terminated, but will not exceed 'maxlen' characters.  The copy *will*
  1049.  be \0-terminated.  */
  1050. static char *
  1051. DEFUN(copy_name,(abfd, name, maxlen),
  1052.       bfd *abfd AND
  1053.       char *name AND
  1054.       int maxlen)
  1055. {
  1056.   int  len;
  1057.   char *newname;
  1058.  
  1059.   for (len = 0; len < maxlen; ++len) {
  1060.     if (name[len] == '\0') {
  1061.       break;
  1062.     }
  1063.   }
  1064.  
  1065.   if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) {
  1066.     bfd_error = no_memory;
  1067.     return (NULL);
  1068.   }
  1069.   strncpy(newname, name, len);
  1070.   newname[len] = '\0';
  1071.   return newname;
  1072. }
  1073.  
  1074. /* Read a symbol table into freshly bfd_allocated memory, swap it, and
  1075.    knit the symbol names into a normalized form.  By normalized here I
  1076.    mean that all symbols have an n_offset pointer that points to a null-
  1077.    terminated string.  */
  1078.  
  1079. combined_entry_type *
  1080. DEFUN(coff_get_normalized_symtab,(abfd),
  1081. bfd            *abfd)
  1082. {
  1083.   combined_entry_type          *internal;
  1084.   combined_entry_type          *internal_ptr;
  1085.   combined_entry_type          *symbol_ptr;
  1086.   combined_entry_type         *internal_end;
  1087.   bfd_size_type symesz;
  1088.   PTR raw;
  1089.   char *raw_src;
  1090.   char *raw_end;
  1091.   char           *string_table = NULL;
  1092.   char         *debug_section = NULL;
  1093.   unsigned long   size;
  1094.  
  1095.   unsigned int raw_size;
  1096.   if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) {
  1097.       return obj_raw_syments(abfd);
  1098.     }
  1099.   if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) {
  1100.       bfd_error = no_symbols;
  1101.       return (NULL);
  1102.     }
  1103.  
  1104.   internal = (combined_entry_type *)bfd_alloc(abfd, size);
  1105.   internal_end = internal + bfd_get_symcount(abfd);
  1106.  
  1107.   symesz = bfd_coff_symesz (abfd);
  1108.   raw_size =      bfd_get_symcount(abfd) * symesz;
  1109.   raw = bfd_alloc(abfd,raw_size);
  1110.  
  1111.   if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
  1112.       || bfd_read(raw, raw_size, 1, abfd) != raw_size) {
  1113.       bfd_error = system_call_error;
  1114.       return (NULL);
  1115.     }
  1116.   /* mark the end of the symbols */
  1117.   raw_end = (char *) raw + bfd_get_symcount(abfd) * symesz;
  1118.   /*
  1119.     FIXME SOMEDAY.  A string table size of zero is very weird, but
  1120.     probably possible.  If one shows up, it will probably kill us.
  1121.     */
  1122.  
  1123.   /* Swap all the raw entries */
  1124.   for (raw_src = (char *) raw, internal_ptr = internal;
  1125.        raw_src < raw_end;
  1126.        raw_src += symesz, internal_ptr++) {
  1127.  
  1128.       unsigned int i;
  1129.       bfd_coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment);
  1130.       internal_ptr->fix_tag = 0;
  1131.       internal_ptr->fix_end = 0;
  1132.       symbol_ptr = internal_ptr;
  1133.  
  1134.       for (i = 0;
  1135.        i < symbol_ptr->u.syment.n_numaux;
  1136.        i++) 
  1137.       {
  1138.     internal_ptr++;
  1139.     raw_src += symesz;
  1140.       
  1141.     internal_ptr->fix_tag = 0;
  1142.     internal_ptr->fix_end = 0;
  1143.     bfd_coff_swap_aux_in(abfd, (PTR) raw_src,
  1144.                  symbol_ptr->u.syment.n_type,
  1145.                  symbol_ptr->u.syment.n_sclass,
  1146.                  &(internal_ptr->u.auxent));
  1147.     /* Remember that bal entries arn't pointerized */
  1148.     if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC)
  1149.     {
  1150.       
  1151.     coff_pointerize_aux(abfd,
  1152.                 internal,
  1153.                 symbol_ptr->u.syment.n_type,
  1154.                 symbol_ptr->u.syment.n_sclass,
  1155.                 internal_ptr);
  1156.       }
  1157.     
  1158.       }
  1159.     }
  1160.  
  1161.   /* Free all the raw stuff */
  1162.   bfd_release(abfd, raw);
  1163.  
  1164.   for (internal_ptr = internal; internal_ptr < internal_end;
  1165.        internal_ptr ++)
  1166.   {
  1167.     if (internal_ptr->u.syment.n_sclass == C_FILE) {
  1168.     /* make a file symbol point to the name in the auxent, since
  1169.        the text ".file" is redundant */
  1170.     if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) {
  1171.         /* the filename is a long one, point into the string table */
  1172.         if (string_table == NULL) {
  1173.         string_table = build_string_table(abfd);
  1174.           }
  1175.  
  1176.         internal_ptr->u.syment._n._n_n._n_offset =
  1177.          (int) (string_table - 4 +
  1178.             (internal_ptr+1)->u.auxent.x_file.x_n.x_offset);
  1179.       }
  1180.     else {
  1181.         /* ordinary short filename, put into memory anyway */
  1182.         internal_ptr->u.syment._n._n_n._n_offset = (int)
  1183.          copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname,
  1184.                FILNMLEN);
  1185.       }
  1186.       }
  1187.     else {
  1188.     if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) {
  1189.         /* This is a "short" name.  Make it long.  */
  1190.         unsigned long   i = 0;
  1191.         char           *newstring = NULL;
  1192.  
  1193.         /* find the length of this string without walking into memory
  1194.            that isn't ours.  */
  1195.         for (i = 0; i < 8; ++i) {
  1196.         if (internal_ptr->u.syment._n._n_name[i] == '\0') {
  1197.             break;
  1198.           }        /* if end of string */
  1199.           }            /* possible lengths of this string. */
  1200.  
  1201.         if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
  1202.         bfd_error = no_memory;
  1203.         return (NULL);
  1204.           }            /* on error */
  1205.         memset(newstring, 0, i);
  1206.         strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1);
  1207.         internal_ptr->u.syment._n._n_n._n_offset =  (int) newstring;
  1208.         internal_ptr->u.syment._n._n_n._n_zeroes = 0;
  1209.       }
  1210.     else if (!bfd_coff_symname_in_debug(abfd, &internal_ptr->u.syment)) {
  1211.         /* Long name already.  Point symbol at the string in the table.  */
  1212.         if (string_table == NULL) {
  1213.         string_table = build_string_table(abfd);
  1214.           }
  1215.         internal_ptr->u.syment._n._n_n._n_offset = (int)
  1216.          (string_table - 4 + internal_ptr->u.syment._n._n_n._n_offset);
  1217.       }
  1218.     else {
  1219.         /* Long name in debug section.  Very similar.  */
  1220.         if (debug_section == NULL) {
  1221.         debug_section = build_debug_section(abfd);
  1222.           }
  1223.         internal_ptr->u.syment._n._n_n._n_offset = (int)
  1224.          (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
  1225.       }
  1226.       }
  1227.     internal_ptr += internal_ptr->u.syment.n_numaux;
  1228.   }
  1229.  
  1230.   obj_raw_syments(abfd) = internal;
  1231.   obj_raw_syment_count(abfd) = internal_ptr - internal;
  1232.  
  1233.   return (internal);
  1234. }                /* coff_get_normalized_symtab() */
  1235.  
  1236. unsigned int
  1237. DEFUN (coff_get_reloc_upper_bound, (abfd, asect),
  1238.        bfd            *abfd AND
  1239.        sec_ptr         asect)
  1240. {
  1241.   if (bfd_get_format(abfd) != bfd_object) {
  1242.     bfd_error = invalid_operation;
  1243.     return 0;
  1244.   }
  1245.   return (asect->reloc_count + 1) * sizeof(arelent *);
  1246. }
  1247.  
  1248. asymbol *
  1249. DEFUN (coff_make_empty_symbol, (abfd),
  1250.        bfd            *abfd)
  1251. {
  1252.   coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
  1253.   if (new == NULL) {
  1254.     bfd_error = no_memory;
  1255.     return (NULL);
  1256.   }                /* on error */
  1257.   new->symbol.section = 0;
  1258.   new->native = 0;
  1259.   new->lineno = (alent *) NULL;
  1260.   new->done_lineno = false;
  1261.   new->symbol.the_bfd = abfd;
  1262.   return &new->symbol;
  1263. }
  1264.  
  1265. /* Make a debugging symbol.  */
  1266.  
  1267. asymbol *
  1268. coff_bfd_make_debug_symbol (abfd, ptr, sz)
  1269.      bfd *abfd;
  1270.      PTR ptr;
  1271.      unsigned long sz;
  1272. {
  1273.   coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
  1274.   if (new == NULL) {
  1275.     bfd_error = no_memory;
  1276.     return (NULL);
  1277.   }                /* on error */
  1278.   /* @@ This shouldn't be using a constant multiplier.  */
  1279.   new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
  1280.   new->symbol.section = &bfd_debug_section;
  1281.   new->lineno = (alent *) NULL;
  1282.   new->done_lineno = false;
  1283.   new->symbol.the_bfd = abfd;
  1284.   return &new->symbol;
  1285. }
  1286.  
  1287. /* Print out information about COFF symbol.  */
  1288.  
  1289. void
  1290. coff_print_symbol (abfd, filep, symbol, how)
  1291.      bfd *abfd;
  1292.      PTR filep;
  1293.      asymbol *symbol;
  1294.      bfd_print_symbol_type how;
  1295. {
  1296.   FILE *file = (FILE *) filep;
  1297.  
  1298.   switch (how)
  1299.     {
  1300.     case bfd_print_symbol_name:
  1301.       fprintf (file, "%s", symbol->name);
  1302.       break;
  1303.  
  1304.     case bfd_print_symbol_more:
  1305.       fprintf (file, "coff %s %s",
  1306.            coffsymbol(symbol)->native ? "n" : "g",
  1307.            coffsymbol(symbol)->lineno ? "l" : " ");
  1308.       break;
  1309.  
  1310.     case bfd_print_symbol_nm:
  1311.       bfd_print_symbol_vandf ((PTR) file, symbol);
  1312.       fprintf (file, " %-5s %s %s %s",
  1313.            symbol->section->name,
  1314.            coffsymbol(symbol)->native ? "n" : "g",
  1315.            coffsymbol(symbol)->lineno ? "l" : " ",
  1316.            symbol->name);
  1317.       break;
  1318.  
  1319.     case bfd_print_symbol_all:
  1320.       if (coffsymbol(symbol)->native) 
  1321.     {
  1322.       unsigned int aux;
  1323.       combined_entry_type *combined = coffsymbol (symbol)->native;
  1324.       combined_entry_type *root = obj_raw_syments (abfd);
  1325.       struct lineno_cache_entry *l = coffsymbol(symbol)->lineno;
  1326.     
  1327.       fprintf (file,"[%3d]", combined - root);
  1328.  
  1329.       fprintf (file,
  1330.            "(sc %2d)(fl 0x%02x)(ty %3x)(sc %3d) (nx %d) 0x%08x %s",
  1331.            combined->u.syment.n_scnum,
  1332.            combined->u.syment.n_flags,
  1333.            combined->u.syment.n_type,
  1334.            combined->u.syment.n_sclass,
  1335.            combined->u.syment.n_numaux,
  1336.            combined->u.syment.n_value,
  1337.            symbol->name);
  1338.  
  1339.       for (aux = 0; aux < combined->u.syment.n_numaux; aux++) 
  1340.         {
  1341.           combined_entry_type *auxp = combined + aux + 1;
  1342.           long tagndx;
  1343.  
  1344.           if (auxp->fix_tag)
  1345.         tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
  1346.           else
  1347.         tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
  1348.  
  1349.           fprintf (file, "\n");
  1350.           switch (combined->u.syment.n_sclass)
  1351.         {
  1352.         case C_FILE:
  1353.           fprintf (file, "File ");
  1354.           break;
  1355.         default:
  1356.  
  1357.           fprintf (file, "AUX lnno %d size 0x%x tagndx %d",
  1358.                auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
  1359.                auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
  1360.                tagndx);
  1361.           break;
  1362.         }
  1363.         }
  1364.     
  1365.       if (l)
  1366.         {
  1367.           printf ("\n%s :", l->u.sym->name);
  1368.           l++;
  1369.           while (l->line_number) 
  1370.         {
  1371.           printf ("\n%4d : 0x%x",
  1372.               l->line_number,
  1373.               l->u.offset);
  1374.           l++;
  1375.         }
  1376.         }
  1377.     } 
  1378.       else
  1379.     {
  1380.       bfd_print_symbol_vandf ((PTR) file, symbol);
  1381.       fprintf (file, " %-5s %s %s %s",
  1382.            symbol->section->name,
  1383.            coffsymbol(symbol)->native ? "n" : "g",
  1384.            coffsymbol(symbol)->lineno ? "l" : " ",
  1385.            symbol->name);
  1386.     }
  1387.     }
  1388. }
  1389.  
  1390. /* Provided a BFD, a section and an offset into the section, calculate
  1391.    and return the name of the source file and the line nearest to the
  1392.    wanted location.  */
  1393.  
  1394. boolean
  1395. DEFUN(coff_find_nearest_line,(abfd,
  1396.                   section,
  1397.                   ignore_symbols,
  1398.                   offset,
  1399.                   filename_ptr,
  1400.                   functionname_ptr,
  1401.                   line_ptr),
  1402.       bfd            *abfd AND
  1403.       asection       *section AND
  1404.       asymbol       **ignore_symbols AND
  1405.       bfd_vma         offset AND
  1406.       CONST char      **filename_ptr AND
  1407.       CONST char       **functionname_ptr AND
  1408.       unsigned int   *line_ptr)
  1409. {
  1410.   static bfd     *cache_abfd;
  1411.   static asection *cache_section;
  1412.   static bfd_vma  cache_offset;
  1413.   static unsigned int cache_i;
  1414.   static CONST char *cache_function;
  1415.   static unsigned int    line_base = 0;
  1416.  
  1417.   unsigned int    i = 0;
  1418.   coff_data_type *cof = coff_data(abfd);
  1419.   /* Run through the raw syments if available */
  1420.   combined_entry_type *p;
  1421.   alent          *l;
  1422.  
  1423.  
  1424.   *filename_ptr = 0;
  1425.   *functionname_ptr = 0;
  1426.   *line_ptr = 0;
  1427.  
  1428.   /* Don't try and find line numbers in a non coff file */
  1429.   if (abfd->xvec->flavour != bfd_target_coff_flavour)
  1430.     return false;
  1431.  
  1432.   if (cof == NULL)
  1433.     return false;
  1434.  
  1435.   p = cof->raw_syments;
  1436.  
  1437.   for (i = 0; i < cof->raw_syment_count; i++) {
  1438.     if (p->u.syment.n_sclass == C_FILE) {
  1439.       /* File name has been moved into symbol */
  1440.       *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
  1441.       break;
  1442.     }
  1443.     p += 1 +  p->u.syment.n_numaux;
  1444.   }
  1445.   /* Now wander though the raw linenumbers of the section */
  1446.   /*
  1447.     If this is the same BFD as we were previously called with and this is
  1448.     the same section, and the offset we want is further down then we can
  1449.     prime the lookup loop
  1450.     */
  1451.   if (abfd == cache_abfd &&
  1452.       section == cache_section &&
  1453.       offset >= cache_offset) {
  1454.     i = cache_i;
  1455.     *functionname_ptr = cache_function;
  1456.   }
  1457.   else {
  1458.     i = 0;
  1459.   }
  1460.   l = §ion->lineno[i];
  1461.  
  1462.   for (; i < section->lineno_count; i++) {
  1463.     if (l->line_number == 0) {
  1464.       /* Get the symbol this line number points at */
  1465.       coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
  1466.       if (coff->symbol.value > offset)
  1467.     break;
  1468.       *functionname_ptr = coff->symbol.name;
  1469.       if (coff->native) {
  1470.     combined_entry_type  *s = coff->native;
  1471.     s = s + 1 + s->u.syment.n_numaux;
  1472.     /*
  1473.       S should now point to the .bf of the function
  1474.       */
  1475.     if (s->u.syment.n_numaux) {
  1476.       /*
  1477.         The linenumber is stored in the auxent
  1478.         */
  1479.       union internal_auxent   *a = &((s + 1)->u.auxent);
  1480.       line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
  1481.       *line_ptr = line_base;
  1482.     }
  1483.       }
  1484.     }
  1485.     else {
  1486.       if (l->u.offset > offset)
  1487.     break;
  1488.       *line_ptr = l->line_number + line_base - 1;
  1489.     }
  1490.     l++;
  1491.   }
  1492.  
  1493.   cache_abfd = abfd;
  1494.   cache_section = section;
  1495.   cache_offset = offset;
  1496.   cache_i = i;
  1497.   cache_function = *functionname_ptr;
  1498.  
  1499.   return true;
  1500. }
  1501.  
  1502. int
  1503. DEFUN(coff_sizeof_headers,(abfd, reloc),
  1504.       bfd *abfd AND
  1505.       boolean reloc)
  1506. {
  1507.     size_t size;
  1508.  
  1509.     if (reloc == false) {
  1510.     size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
  1511.     }
  1512.     else {
  1513.     size = bfd_coff_filhsz (abfd);
  1514.     }
  1515.  
  1516.     size +=  abfd->section_count * bfd_coff_scnhsz (abfd);
  1517.     return size;
  1518. }
  1519.